
import urllib2
import sys
from decimal import *
from BeautifulSoup import BeautifulSoup

class Scraper:
    
    def getReviews( self, account_id ):
        pass
    def getSummary( self, account_id ):
        pass
        
    def __init__(self):
        pass
        
        
class EbayScraper( Scraper ):
    baseURLHeader = "http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback2&userid="
    baseURLTrailer = "&ftab=AllFeedback"
    baseXMLHeader = "http://ebayodata.cloudapp.net/Users('"
    baseXMLTrailer = "')"
    
    def getReviews( self, account_id ):
        try:
            ret = []
            page = urllib2.urlopen( self.baseURLHeader + str(account_id) + self.baseURLTrailer )
            soup = BeautifulSoup(page)
            table = soup.find('table', {"class" : "FbOuterYukon" })
            tableRows = table.findAll( 'tr' )
            for row in tableRows:
                mbg_nw = row.find('span', {"class":"mbg-nw"})
                if mbg_nw != None:
                    my_score = 0
                    rating  = row.find('img')['alt']
                    if rating == "Positive feedback rating":
                        my_score = 1
                        
                    user    = mbg_nw.string
                    message = row.contents[1].string
                    date    = row.contents[3].string
                    ret.append( dict(reviewer=user, review=message, review_date=date, score=my_score ) )
            return ret
        
        except:
            print "EbayScraper.getReviews: Unexpected error:", sys.exc_info()[0]
            raise
    
    def getSummary( self, account_id ):
        try:
            page = urllib2.urlopen( self.baseXMLHeader + str(account_id) + self.baseXMLTrailer )
            soup = BeautifulSoup(page)
            positivepercent = soup.find('d:positivefeedbackpercent').string
            totalreviews    = soup.find('d:feedbackscore').string
            positivereviews = int(positivepercent)*int(totalreviews)/100
            return dict(totalReviews=int(totalreviews), positiveReviews=positivereviews)
        
        except: 
            print "EbayScraper.getSummary: Unexpected error:", sys.exc_info()[0]
            raise        
        
    def __init__(self):
        pass
       
class GetAroundScraper( Scraper ):
    
    baseURL = "http://www.getaround.com/"
    
    def getReviews( self, account_id ):
        ret = []
        try:      
            page = urllib2.urlopen( self.baseURL + account_id )
            soup = BeautifulSoup(page)
            tempSoup = soup.find('div', {"id" : "ratingsAndReviews"})
            for review in tempSoup.findAll('div', {"class" : "detailed_rating"}) :
                name = review.find('a', {"class" : "nolink"}).string
                date = review.find('div', {"class" : "dateBox"}).string
                message = review.find('div', {"class" : "feedbackBox"}).string
                stars = 0
                for star in review.findAll('img') :
                    stars = stars + 1           
                rating = stars
                ret.append( dict( reviewer=name, score=rating, review_date=date, review=message ))
        except:
            print "GetAroundScraper.getReviews: Unexpected error:", sys.exc_info()[0]
            raise
           
        return ret
        
    def getSummary( self, account_id ):
        try:      
            page = urllib2.urlopen( self.baseURL + account_id )
            soup = BeautifulSoup(page)
            tempSoup = soup.find('div', {"id" : "ratingsAndReviews"})
            allReviews = 0
            goodReviews = 0
            for rating in tempSoup.findAll('div', {"class" : "detailed_rating"}) :
                allReviews = allReviews + 1
                stars = 0
                for star in rating.findAll('img') :
                    stars = stars + 1           
                if( stars >= 3) :
                    goodReviews = goodReviews + 1
                
        except:
            print "GetAroundScraper.getSummary: Unexpected error:", sys.exc_info()[0]
            raise
            
        return dict(positiveReviews=goodReviews, totalReviews=allReviews)
    
    def __init__(self):
        pass
    
    
class AirBnBScraper( Scraper ):

    baseURL = "http://www.airbnb.com/users/show/"
    
    def getReviews( self, account_id ):
        ret = []
        try:
            page = urllib2.urlopen( self.baseURL + str(account_id) )
            soup = BeautifulSoup(page)
            for review in soup.findAll('li', { "class" : "reviews-list-item clearfix" }):
                name = review.find('span', {"class" : "name" }).a.string
                date = review.find('span', {"class" : "date" })
                message = review.find('p', {"class": "message" } ).contents[0].string
                if ( date != None ):
                    date = date.string
                else:
                    date =""
                    
                ret.append( dict( reviewer=name, score=1, review_date=date, review=message ))
                
            return ret    
        except:
            print "AirBnBScraper.getReviews: Unexpected error:", sys.exc_info()[0]
            raise
        
        
    
    def getSummary( self, account_id ):
        try:
            page = urllib2.urlopen( self.baseURL + str(account_id) )
            soup = BeautifulSoup(page)
            tempSoup = soup.find('div', {"class" : "panel verifications"})
            for item in tempSoup.findAll('li', {"class" : "verifications-list-item"}) :
                if (item.find('span', {"class" : "label"}).string == "Positively reviewed") :
                    goodReviews = int(item.find('span', {'class' : 'count'}).string.split()[0])
            tempSoup = soup.find('li', {"class" : "badge reviews"})
            allReviews = int(tempSoup.div.string)
        except:
            print "AirBnBScraper.getSummary: Unexpected error:", sys.exc_info()[0]
            raise
        
        return dict(positiveReviews=goodReviews, totalReviews=allReviews)
           
    def __init__(self):
        pass

def main():
    abbs = AirBnBScraper()
    #summary = abbs.getSummary(105888)
    reviews = abbs.getReviews(105888)
    i = 0
    for review in reviews :
        i = i + i
        print "\t\Review:", i
        print "\t\t\tName: ", review['reviewer']
        print "\t\t\tDate: ", review['review_date']
        print "\t\t\tScore: ", int(review['score'])
        print "\t\t\tReview: ", review['review']
        
    #goodReviews = summary['postiveReviews']
    #allReviews  = summary['totalReviews']
    #print "AirBnB: ", goodReviews, " good reviews out of ", allReviews, " total reviews."    
    
    """
    getAroundScrapper = GetAroundScraper()
    summary = getAroundScrapper.getSummary('tacoma24')
    reviews = getAroundScrapper.getReviews('tacoma24')
    goodReviews = summary['postiveReviews']
    allReviews  = summary['totalReviews']
    print "GetAround: "
    print "\tSummary: ", goodReviews, " good reviews out of ", allReviews, " total reviews."    
    print "\tReviews:"
    i = 0
    for review in reviews :
        print "\t\tReview ", i
        print "\t\t\tName: ", review['reviewer']
        print "\t\t\tDate: ", review['review_date']
        print "\t\t\tScore: ", Decimal(review['score'])
        print "\t\t\tReview: ", review['review']
    """
        
if __name__ == '__main__':
    main()
